use axum::{http::StatusCode};
use serde_json::json;
use super::handlers::{health_check, proxy_http, HealthResponse, ProxyRequest, ProxyResponse};

#[cfg(test)]
mod tests {
    use super::*;
    use axum::extract::Json as AxumJson;
    use std::collections::HashMap;

    #[tokio::test]
    async fn test_health_check_success() {
        // 测试健康检查端点
        let (status, response) = health_check().await;
        
        // 验证状态码
        assert_eq!(status, StatusCode::OK);
        
        // 验证响应内容
        let health_response: HealthResponse = response.0;
        assert_eq!(health_response.status, "ok");
        assert_eq!(health_response.message, "服务运行正常");
    }

    #[tokio::test]
    async fn test_health_check_response_structure() {
        // 测试健康检查响应的数据结构
        let (status, response) = health_check().await;
        
        assert_eq!(status, StatusCode::OK);
        
        // 验证JSON序列化
        let json_value = serde_json::to_value(&response.0).unwrap();
        assert!(json_value.is_object());
        assert_eq!(json_value["status"], "ok");
        assert_eq!(json_value["message"], "服务运行正常");
    }

    #[tokio::test]
    async fn test_proxy_http_success() {
        // 测试HTTP代理端点 - 基本请求
        let request = ProxyRequest {
            url: "https://example.com/api/test".to_string(),
            method: "GET".to_string(),
            headers: None,
            body: None,
        };
        
        let result = proxy_http(AxumJson(request)).await;
        
        // 验证返回成功
        assert!(result.is_ok());
        
        let response = result.unwrap().0;
        assert_eq!(response.status, 200);
        assert_eq!(response.body, "代理请求已接收");
        assert!(response.headers.is_empty());
    }

    #[tokio::test]
    async fn test_proxy_http_with_headers() {
        // 测试HTTP代理端点 - 带请求头
        let mut headers = HashMap::new();
        headers.insert("Content-Type".to_string(), "application/json".to_string());
        headers.insert("Authorization".to_string(), "Bearer token123".to_string());
        
        let request = ProxyRequest {
            url: "https://api.example.com/data".to_string(),
            method: "POST".to_string(),
            headers: Some(headers),
            body: Some(json!({"key": "value"}).to_string()),
        };
        
        let result = proxy_http(AxumJson(request)).await;
        
        assert!(result.is_ok());
        
        let response = result.unwrap().0;
        assert_eq!(response.status, 200);
        assert_eq!(response.body, "代理请求已接收");
    }

    #[tokio::test]
    async fn test_proxy_http_different_methods() {
        // 测试HTTP代理端点 - 不同HTTP方法
        let methods = vec!["GET", "POST", "PUT", "DELETE", "PATCH"];
        
        for method in methods {
            let request = ProxyRequest {
                url: format!("https://example.com/api/{}", method),
                method: method.to_string(),
                headers: None,
                body: None,
            };
            
            let result = proxy_http(AxumJson(request)).await;
            assert!(result.is_ok());
            
            let response = result.unwrap().0;
            assert_eq!(response.status, 200);
        }
    }

    #[tokio::test]
    async fn test_proxy_http_empty_url() {
        // 测试HTTP代理端点 - 空URL
        let request = ProxyRequest {
            url: "".to_string(),
            method: "GET".to_string(),
            headers: None,
            body: None,
        };
        
        let result = proxy_http(AxumJson(request)).await;
        
        // 当前实现中空URL不会导致错误
        assert!(result.is_ok());
        
        let response = result.unwrap().0;
        assert_eq!(response.status, 200);
    }

    #[tokio::test]
    async fn test_proxy_http_response_structure() {
        // 测试HTTP代理响应的数据结构
        let request = ProxyRequest {
            url: "https://example.com".to_string(),
            method: "GET".to_string(),
            headers: None,
            body: None,
        };
        
        let result = proxy_http(AxumJson(request)).await;
        assert!(result.is_ok());
        
        let response = result.unwrap().0;
        
        // 验证JSON序列化
        let json_value = serde_json::to_value(&response).unwrap();
        assert!(json_value.is_object());
        assert_eq!(json_value["status"], 200);
        assert_eq!(json_value["body"], "代理请求已接收");
        assert!(json_value["headers"].is_object());
    }

    #[tokio::test]
    async fn test_health_response_serialization() {
        // 测试HealthResponse的序列化/反序列化
        let original = HealthResponse {
            status: "healthy".to_string(),
            message: "系统正常".to_string(),
        };
        
        let serialized = serde_json::to_string(&original).unwrap();
        let deserialized: HealthResponse = serde_json::from_str(&serialized).unwrap();
        
        assert_eq!(original.status, deserialized.status);
        assert_eq!(original.message, deserialized.message);
    }

    #[tokio::test]
    async fn test_proxy_request_serialization() {
        // 测试ProxyRequest的序列化/反序列化
        let mut headers = HashMap::new();
        headers.insert("User-Agent".to_string(), "TestClient".to_string());
        
        let original = ProxyRequest {
            url: "https://test.com".to_string(),
            method: "POST".to_string(),
            headers: Some(headers),
            body: Some("test body".to_string()),
        };
        
        let serialized = serde_json::to_string(&original).unwrap();
        let deserialized: ProxyRequest = serde_json::from_str(&serialized).unwrap();
        
        assert_eq!(original.url, deserialized.url);
        assert_eq!(original.method, deserialized.method);
        assert_eq!(original.body, deserialized.body);
        assert_eq!(original.headers, deserialized.headers);
    }

    #[tokio::test]
    async fn test_proxy_response_serialization() {
        // 测试ProxyResponse的序列化/反序列化
        let mut headers = HashMap::new();
        headers.insert("Content-Type".to_string(), "application/json".to_string());
        
        let original = ProxyResponse {
            status: 201,
            headers,
            body: "created".to_string(),
        };
        
        let serialized = serde_json::to_string(&original).unwrap();
        let deserialized: ProxyResponse = serde_json::from_str(&serialized).unwrap();
        
        assert_eq!(original.status, deserialized.status);
        assert_eq!(original.body, deserialized.body);
        assert_eq!(original.headers, deserialized.headers);
    }

    #[test]
    fn test_struct_debug_impl() {
        // 测试结构体的Debug实现
        let health = HealthResponse {
            status: "debug".to_string(),
            message: "test message".to_string(),
        };
        
        let debug_output = format!("{:?}", health);
        assert!(debug_output.contains("HealthResponse"));
        assert!(debug_output.contains("debug"));
        assert!(debug_output.contains("test message"));
        
        let proxy_req = ProxyRequest {
            url: "test".to_string(),
            method: "GET".to_string(),
            headers: None,
            body: None,
        };
        
        let debug_output = format!("{:?}", proxy_req);
        assert!(debug_output.contains("ProxyRequest"));
        assert!(debug_output.contains("test"));
    }
}